{#
    Creates a list entry
    * `href` A string used as the tab's link
    * `text` A string used as the tab's text
    * `target` An optional target
    * `extra_classes` Optional extra css classes
#}
{% macro menu_link(href, text, target, icon="") -%}
    <li class="pure-menu-item"> {#- -#}
        <a class="pure-menu-link" href="{{ href }}"{% if !target.is_empty() %} target="{{ target }}" {%- endif %}>{{ text }}</a> {#- -#}
    </li>
{%- endmacro menu_link %}

{% macro menu_link_with_icon_solid(href, text, icon, target="") -%}
    <li class="pure-menu-item"> {#- -#}
        <a class="pure-menu-link" href="{{ href }}"{% if !target.is_empty() %} target="{{ target }}" {%- endif %}>
            {{- icon.render_solid(false, false, "") }} {{ text -}}
        </a> {#- -#}
    </li>
{%- endmacro menu_link_with_icon_solid %}

{% macro menu_link_with_icon_brand(href, text, icon) -%}
    <li class="pure-menu-item"> {#- -#}
        <a class="pure-menu-link" href="{{ href }}">
            {{- icon.render_brands(false, false, "") }} {{ text -}}
        </a> {#- -#}
    </li>
{%- endmacro menu_link_with_icon_brand %}

{#
    Creates a formatted table showing the resource limits of a crate
    * `limits` A non-null `Limits` struct
#}
{% macro crate_limits(limits) %}
    <table class="pure-table pure-table-horizontal">
        <tbody>
            <tr>
                <td>Available RAM</td>
                <td>{{ limits.memory|filesizeformat }}</td>
            </tr>

            <tr>
                <td>Maximum rustdoc execution time</td>
                <td>{{ limits.timeout.as_secs_f32()|format_secs }}</td>
            </tr>

            <tr>
                <td>Maximum size of a build log</td>
                <td>{{ limits.max_log_size|filesizeformat }}</td>
            </tr>

            <tr>
                <td>Network access</td>
                <td>
                    {%- if limits.networking -%}
                        allowed
                    {%- else -%}
                        blocked
                    {%- endif -%}
                </td>
            </tr>

            <tr>
                <td>Maximum number of build targets</td>
                <td>{{ limits.targets }}</td>
            </tr>
        </tbody>
    </table>
{% endmacro crate_limits %}

{# Constructs a title based on the given crate name and version #}
{% macro doc_title(name, version) %}
    {%- if !name.is_empty() -%}
        {{ name }} {{ version }} - Docs.rs
    {%- else -%}
        Docs.rs
    {%- endif -%}
{% endmacro doc_title %}

{#
    Constructs a list of a crate's releases
    * `params`: `RustdocParams` to access the functions to generate URLs.
    * `releases` A list of crate releases where each release has the following fields:
        * `version` A string of the release's version
        * `yanked` A boolean of the release's yanked status
        * `build_status` A string of the crate's build status
          * "success" for built
          * "failure" for failed build
          * "in_progress" for in progress
        * `is_library` A boolean that's true if the crate is a library and false if it's a binary
    * `use_target_redirect`: either link to the crate-details page, or a target-redirect
    * `retain_fragment`: if the link should retain the fragment from the current page
#}
{% macro releases_list(params, releases, use_target_redirect, retain_fragment) %}
    {%- for release in releases -%}
        {%- set release_params = params.clone().with_req_version(release.version.clone()) -%}

        {# The url for the release, `/crate/:name/:version` #}
        {% set release_url %}
        {% if use_target_redirect %}
            {%- set release_url = release_params.target_redirect_url() -%}
        {% else %} {# /crate #}
            {%- set release_url = release_params.crate_details_url() -%}
        {% endif %}
        {# The release's name and version, `:name-:version` #}
        {%- set release_name = "{}-{}"|format(params.name(), release.version) -%}
        {%- set warning -%}
        {%- set title -%}
        {%- set yanked = release.yanked.unwrap_or_default() -%}

        {%- if !release.is_library.unwrap_or_default() -%}
            {# If the release isn't a library, then display that warning #}
            {%- set warning = true -%}
            {%- set title = "{} is not a library"|format(release_name) -%}
        {%- elif yanked && release.build_status == "success" -%}
            {# If the release has been yanked and failed to build, display a warning #}
            {%- set warning = true -%}
            {%- set title = "{} is yanked"|format(release_name) -%}
        {%- elif yanked && release.build_status == "failure" -%}
            {# If the release has been yanked and failed to build, display a warning #}
            {%- set warning = true -%}
            {%- set title = "{} is yanked and docs.rs failed to build it"|format(release_name) -%}
        {%- elif release.build_status == "failure" -%}
            {# If the release failed to build, display a warning #}
            {%- set warning = true -%}
            {%- set title = "docs.rs failed to build {}"|format(release_name) -%}
        {%- elif release.build_status == "in_progress" -%}
            {%- set warning = false -%}
            {%- set title = "{} is currently being built"|format(release_name) -%}
        {%- else -%}
            {%- set warning = false -%}
            {%- set title = String::new() -%}
        {%- endif -%}

        <li class="pure-menu-item">
            <a
                href="{{ release_url|safe }}"
                {# We only want crawlers to crawl the /latest/ URLs, not /1.2.3/ URLs. #}
                rel="nofollow"
                class="pure-menu-link{% if warning %} warn{% endif %}"
                {% if !title.is_empty() %} title="{{ title }}"{% endif %}
                {% if retain_fragment %}data-fragment="retain"{% endif %}
            >
                {%- if warning -%}
                    {{ crate::icons::IconTriangleExclamation.render_solid(false, false, "") -}}
                {%+ endif -%}
                {% if release.build_status == "in_progress" -%}
                    {{ crate::icons::IconGear.render_solid(true, true, "") -}}
                {%+ endif -%}
                <b>{{ release.version }}</b>
                {%- if let Some(release_time) = release.release_time -%}
                    {# +#} ({{ release_time.format("%Y-%m-%d") }})
                {%- endif -%}
            </a>
        </li>
    {%- endfor -%}
{% endmacro releases_list %}

{% macro dependencies_list(dependencies, use_crate_details, if_empty) %}
    {%- for dep in dependencies -%}
        <li class="pure-menu-item">
    {%- set dependency_params = RustdocParams::new(dep.name.to_owned()).with_req_version(ReqVersion::Semver(dep.req.clone())) -%}
            {%- set href -%}
            {%- if use_crate_details -%}
                {%- set href = dependency_params.crate_details_url() -%}
            {%- else -%}
                {%- set href = dependency_params.rustdoc_url() -%}
            {%- endif -%}
            <a href="{{ href }}" class="pure-menu-link">
                {{ dep.name|safe }} {{ dep.req|safe }}
                {% if let Some(kind) = &dep.kind %}
                    <i class="dependencies {{ kind }}">{{ kind }}</i>
                    {% if dep.optional %}
                        <i>optional</i>
                    {% endif %}
                {% else %}
                    <i class="dependencies"></i>
                {% endif %}
            </a>
        </li>
    {%- endfor -%}
    {%- if dependencies.is_empty() -%}
        {{ if_empty|safe }}
    {%- endif -%}
{% endmacro dependencies_list %}
